<template>
  <div>
    <Search></Search>
    <div class="shop-item-path">
      <div class="shop-nav-container">
        <Breadcrumb>
          <BreadcrumbItem to="/">首页</BreadcrumbItem>
          <BreadcrumbItem>{{ name }}</BreadcrumbItem>
        </Breadcrumb>
      </div>
    </div>
    <!-- 商品信息展示 -->
    <div>
      <div class="item-detail-show">
        <!-- 图片区域 -->
        <div class="item-detail-left">
          <div class="item-detail-big-img">
            <img :src="goodsInfo['imgUrls'][imgIndex]" alt="" />
          </div>
          <div class="item-detail-img-row">
            <div
              class="item-detail-img-small"
              v-for="(item, index) in goodsInfo.imgUrls"
              :key="index"
              @mouseover="showBigImg(index)"
            >
              <img :src="item" alt="" />
            </div>
          </div>
        </div>
        <div class="item-detail-right">
          <div class="item-detail-title">
            <p>
              <span class="item-detail-express">急速配送</span>
              {{ goodsInfo.name }}
            </p>
          </div>
          <div class="item-detail-tag"></div>
          <div class="item-detail-price-row">
            <div class="item-price-left">
              <div class="item-price-row">
                <p>
                  <span class="item-price-title">价 格</span>
                  <span class="item-price">￥{{ goodsInfo.price }}</span>
                </p>
              </div>
            </div>
            <div class="item-price-right">
              <div class="item-remarks-sum">
                <p>库存</p>
                <p>
                  <span class="item-remarks-num">{{ goodsInfo.stock }}</span>
                </p>
              </div>
            </div>
          </div>
          <p class="content">
            <span>描述: </span>
            <span>{{ goodsInfo.content }}</span>
          </p>
          <br />
          <div class="add-buy-car-box">
            <div class="add-buy-car">
              <InputNumber :min="1" v-model="count" size="large"></InputNumber>
              <Button type="error" size="large" @click="addShoppingCartBtn()"
                >加入购物车</Button
              >
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Search from '~/components/search/Search'
import { getGoodsByGoodsId } from '~/api/goods'
import { mapActions } from 'vuex'
export default {
  name: 'GoodsDetail',
  data() {
    return {
      name: '',
      price: 0,
      count: 1,
      imgIndex: 0,
      id: 0,
      goodsInfo: { imgUrls: [] }
    }
  },
  created() {
    this.id = this.$route.query.id
    getGoodsByGoodsId(this.id).then(res => {
      this.goodsInfo = res.data
      this.name = this.goodsInfo.name
    })
  },
  methods: {
    ...mapActions('cart', ['addShoppingCart']),
    showBigImg(index) {
      this.imgIndex = index
    },
    // 添加购物车
    addShoppingCartBtn() {
      // 传递商品id与数量即可，uid由action中获取并发送
      const data = {
        gid: this.id,
        count: this.count
      }
      // 操作vuex的添加操作，并等待操作结果
      this.addShoppingCart(data).then(res => {
        if (res === 'success') {
          this.$router.push('/successfullyAdd')
        } else {
          this.$Message.error('添加失败')
        }
      })
    }
  },
  components: {
    Search
  }
}
</script>

<style scoped>
.shop-item-path {
  height: 38px;
  background-color: rgb(236, 235, 235);
  line-height: 38px;
  color: #2c2c2c;
}
.shop-nav-container {
  margin: 0px auto;
  width: 80%;
}
/******************商品图片及购买详情开始******************/
.item-detail-show {
  width: 80%;
  margin: 15px auto;
  display: flex;
  flex-direction: row;
  background-color: #fff;
}
.item-detail-left {
  width: 700px;
  margin-right: 30px;
}
.item-detail-big-img {
  width: 700px;
  height: 440px;
  box-shadow: 0px 0px 8px #ccc;
  cursor: pointer;
}
.item-detail-big-img img {
  object-fit: contain;
  width: 100%;
  height: 100%;
}
.item-detail-img-row {
  margin-top: 15px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
}
.item-detail-img-small {
  margin-right: 40px;
  width: 68px;
  height: 68px;
  box-shadow: 0px 0px 8px #ccc;
  cursor: pointer;
}
.item-detail-img-small img {
  width: 100%;
  height: 100%;
}
/*商品选购详情*/
.item-detail-right {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  width: 300px;
}
.item-detail-title p {
  color: #666;
  font-size: 20px;
}
.item-detail-express {
  font-size: 14px;
  padding: 2px 3px;
  border-radius: 3px;
  background-color: #e4393c;
  color: #fff;
}
/*商品标签*/
.item-detail-tag {
  font-size: 12px;
  color: #e4393c;
}
/*价格详情等*/
.item-detail-price-row {
  padding: 5px;
  margin-top: 45px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  background-color: #f3f3f3;
}
.item-price-left {
  display: flex;
  flex-direction: column;
}
.item-price-title {
  color: #999999;
  font-size: 14px;
  margin-right: 15px;
}
.item-price-row {
  margin: 15px 0px;
}
.item-price {
  color: #e4393c;
  font-size: 23px;
  cursor: pointer;
}
.item-price-full-cut {
  margin-right: 5px;
  padding: 3px;
  color: #e4393c;
  font-size: 12px;
  background-color: #ffdedf;
  border: 1px dotted #e4393c;
  cursor: pointer;
}
.item-remarks-sum {
  margin-top: 15px;
  padding-left: 8px;
  border-left: 1px solid #ccc;
}
.item-remarks-sum p {
  color: #999999;
  font-size: 12px;
  line-height: 10px;
  text-align: center;
}
.item-remarks-num {
  line-height: 18px;
  color: #005eb7;
}
.item-select {
  display: flex;
  flex-direction: row;
  margin-top: 15px;
}
.item-select-title {
  color: #999999;
  font-size: 14px;
  margin-right: 15px;
}
.item-select-column {
  display: flex;
  flex-direction: column;
}
.item-select-row {
  display: flex;
  flex-direction: row;
  margin-bottom: 8px;
}
.item-select-box {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.item-select-img {
  width: 36px;
}
.item-select-box {
  padding: 5px;
  margin-right: 8px;
  background-color: #f7f7f7;
  border: 0.5px solid #ccc;
  cursor: pointer;
}
.item-select-box:hover {
  border: 0.5px solid #e3393c;
}
.item-select-box-active {
  border: 0.5px solid #e3393c;
}
.item-select-img img {
  width: 100%;
}
.item-select-intro p {
  margin: 0px;
  padding: 5px;
}
.item-select-class {
  padding: 5px;
  margin-right: 8px;
  background-color: #f7f7f7;
  border: 0.5px solid #ccc;
  cursor: pointer;
}
.item-select-class:hover {
  border: 0.5px solid #e3393c;
}
.add-buy-car-box {
  width: 100%;
  margin-top: 65px;
  border-top: 1px dotted #ccc;
}
.add-buy-car {
  margin-top: 15px;
}

.content {
  margin-top: 20px;
  font-size: 16px;
}
/******************商品图片及购买详情结束******************/
</style>
